package com.creditharmony.car.loan.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.creditharmony.car.common.base.bean.out.BaseDataOutBean;
import com.creditharmony.car.common.base.controller.GatewayBaseController;
import com.creditharmony.car.common.common.vo.requestvo.CarLoanInfoRequestVo;
import com.creditharmony.car.common.common.vo.responsevo.CarLoanInfoResponseVo;
import com.creditharmony.car.common.exception.UserException;
import com.creditharmony.car.common.utils.ControllerUtils;
import com.creditharmony.car.common.utils.ErrorType;
import com.creditharmony.car.common.utils.MicroServiceLogger;
import com.creditharmony.car.loan.service.CarLoanInfoService;


/**
 * 
 * @Class Name CarLoanInfoController
 * @author zqa
 * @Create In 2018年1月23日
 */
@RestController
@RequestMapping("/carLoanInfo")
public class CarLoanInfoController extends GatewayBaseController {
    /** 处理名称. */
    public static final String PROC_NAME = "借款信息处理";
    /** 日志 */
    private static final Logger logger = LoggerFactory.getLogger(CarLoanInfoController.class);
    /** 日志采集. */
    @Autowired
    private MicroServiceLogger microServiceLogger;
    @Autowired
    private CarLoanInfoService carLoanInfoService;

    /**
     * 保存借款信息
     * 2018年1月23日
     * By zqa
     * @param vo 请求参数
     * @param result 错误信息
     * @param request 请求
     * @return BaseDataOutBean
     */
    @CrossOrigin
    @RequestMapping(value = "saveLoanInfo")
    public BaseDataOutBean<CarLoanInfoResponseVo> saveLoanInfo(@Valid @RequestBody CarLoanInfoRequestVo vo,
            BindingResult result, HttpServletRequest request) {
        BaseDataOutBean<CarLoanInfoResponseVo> outBean = null;
        Map<String,List<String>> errorMsg = parseErrorMsg(result);
        if(errorMsg != null){
            outBean = new BaseDataOutBean<CarLoanInfoResponseVo>(ErrorType.VALIDATE_ERROR, vo.getSerialNum());
            outBean.setErrorMsg(errorMsg);
            return outBean;
        }
        // 初始化请求数据
        ControllerUtils.initBaseInfo(vo, serviceIp, request);
        // 日志:请求
        microServiceLogger.doInLogger(logger, vo, PROC_NAME);
        
        try {
            carLoanInfoService.saveLoanInfo(vo);
            outBean = new BaseDataOutBean<CarLoanInfoResponseVo>(ErrorType.NONE, vo.getSerialNum());
        } catch (Exception e) {
            if (e instanceof UserException) {
                outBean = new BaseDataOutBean<CarLoanInfoResponseVo>(ErrorType.FAIL_BUSSINESS, vo.getSerialNum());
                logger.info(e.getMessage());
            } else {
                logger.info(ErrorType.ERROR_SYS_DB.getMessage());
                outBean = new BaseDataOutBean<CarLoanInfoResponseVo>(ErrorType.ERROR_SYS_DB);
            }
            // 日志:异常
            microServiceLogger.doErrorLogger(logger, outBean, PROC_NAME, e.getMessage(), e);
        }
        // 日志:返回
        microServiceLogger.doOutLogger(logger, outBean, PROC_NAME);
        return outBean;
    }
    /**
     * 修改借款信息
     * 2018年1月30日
     * By zqa
     * @param vo 借款vo
     * @param result 校验错误信息
     * @param request 请求
     * @return BaseDataOutBean<CarLoanInfoResponseVo>
     */
    @CrossOrigin
    @RequestMapping(value = "updateLoanInfo")
    public BaseDataOutBean<CarLoanInfoResponseVo> updateLoanInfo(@Valid @RequestBody CarLoanInfoRequestVo vo,
            BindingResult result, HttpServletRequest request) {
        BaseDataOutBean<CarLoanInfoResponseVo> outBean = null;
        Map<String,List<String>> errorMsg = parseErrorMsg(result);
        if(errorMsg != null){
            outBean = new BaseDataOutBean<CarLoanInfoResponseVo>(ErrorType.VALIDATE_ERROR, vo.getSerialNum());
            outBean.setErrorMsg(errorMsg);
            return outBean;
        }
        // 初始化请求数据
        ControllerUtils.initBaseInfo(vo, serviceIp, request);
        // 日志:请求
        microServiceLogger.doInLogger(logger, vo, PROC_NAME);
        try {
            carLoanInfoService.updateLoanInfo(vo);
            outBean = new BaseDataOutBean<CarLoanInfoResponseVo>(ErrorType.NONE, vo.getSerialNum());
        } catch (Exception e) {
            if (e instanceof UserException) {
                outBean = new BaseDataOutBean<CarLoanInfoResponseVo>(ErrorType.FAIL_BUSSINESS, vo.getSerialNum());
                logger.error(e.getMessage());
            } else {
                logger.error(ErrorType.ERROR_SYS_DB.getMessage());
                outBean = new BaseDataOutBean<CarLoanInfoResponseVo>(ErrorType.ERROR_SYS_DB);
            }
            // 日志:异常
            microServiceLogger.doErrorLogger(logger, outBean, PROC_NAME, e.getMessage(), e);
        }
        // 日志:返回
        microServiceLogger.doOutLogger(logger, outBean, PROC_NAME);
        return outBean;
    }
    
    /**
     * 
     * 2018年1月24日
     * By zqa
     * @param loanCode 借款编号
     * @param request 请求
     * @return BaseDataOutBean<CarLoanInfoResponseVo>
     */
    @CrossOrigin
    @RequestMapping(value = "/queryCarLoanInfoByLoanCode/{loanCode}")
    public BaseDataOutBean<CarLoanInfoResponseVo> queryCarLoanInfoByLoanCode(@PathVariable("loanCode") String loanCode, HttpServletRequest request){
        BaseDataOutBean<CarLoanInfoResponseVo> outBean = null;
        if(StringUtils.isEmpty(loanCode)){
            outBean = new BaseDataOutBean<CarLoanInfoResponseVo>(ErrorType.FAIL_BUSSINESS);
            outBean.setRetMsg("借款编号不能为空");
            return outBean;
        }
        CarLoanInfoResponseVo resVo;
        try {
            resVo = carLoanInfoService.queryCarLoanInfoByLoanCode(loanCode);
            outBean = new BaseDataOutBean<CarLoanInfoResponseVo>(ErrorType.NONE);
            outBean.setData(resVo);
        } catch (Exception e) {
            logger.info(e.getMessage());
            outBean = new BaseDataOutBean<CarLoanInfoResponseVo>(ErrorType.ERROR_SYS_DB);
        }
        return outBean;
    }
}
